'''

functools模块中的高阶函数
'''

import functools
from functools import reduce
import itertools



nums = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, nums)
print(squares)


from functools import reduce

nums = [1, 2, 3, 4, 5]

# reduce 函数用于对可迭代对象中的元素进行累积操作，返回一个单一的值。
result = reduce(lambda x, y: x + y, nums)
print(result)  # 输出: 15


from functools import partial

def power(base, exponent):
    return base ** exponent

square = partial(power, exponent=2)
print(square(3))  # 输出: 9


from functools import lru_cache

@lru_cache(maxsize=128)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))  # 输出: 55


from functools import wraps

def my_decorator(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        print("Before function call")
        result = func(*args, **kwargs)
        print("After function call")
        return result
    return wrapper

@my_decorator
def example():
    print("Inside function")

example()
print(example.__name__)  # 输出: example


from functools import total_ordering

@total_ordering
class Person:
    def __init__(self, age):
        self.age = age

    def __eq__(self, other):
        return self.age == other.age

    def __lt__(self, other):
        return self.age < other.age

p1 = Person(20)
p2 = Person(30)
print(p1 < p2)  # 输出: True
print(p1 >= p2) # 输出: False
